package com.hexacta.hrs.report.managers;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.vectrics.common.util.DateUtil;
import org.vectrics.employee.Employee;

import com.hexacta.hrs.report.AbstractReport;
import com.hexacta.hrs.report.dtos.EmployeeListReportDTO;
import com.hexacta.hrs.services.employee.EmployeeService;

/**
 * Clase que represante el Reporte de Empleados.
 * 
 * @author mfunes
 * 
 */
public class EmployeeListReport extends AbstractReport {

    private EmployeeService employeeService;

    // private AvailableLicenseDaysService availableLicenseDaysService;

    public EmployeeService getEmployeeService() {
        return employeeService;
    }

    public void setEmployeeService(final EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    /** {@inheritDoc} */
    public ByteArrayOutputStream createPDFReport(final String reportName, final String reportTemplateLocation,
            final Map<String, Object> filters, final Map<String, Object> parameters) {
        return createPDF(reportTemplateLocation, parameters, getReportData(filters));
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.hexacta.hrs.report.Report#createXLSReport(java.lang.String,
     * java.lang.String)
     */
    public ByteArrayOutputStream createXLSReport(final String reportName, final String reportTemplateLocation,
            final Map<String, Object> filters, final Map<String, Object> parameters) {
        return createExcel(reportTemplateLocation, parameters, getReportData(filters));
    }

    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    @Override
    protected Collection getReportData(final Map<String, Object> filters) {
        Collection<EmployeeListReportDTO> collection = new ArrayList<EmployeeListReportDTO>();
        Collection<Employee> collectionEmployee = employeeService.getAll();
        collectionEmployee.removeAll(employeeService.getNoLongerEmployees());
        String pivotAux;
        for (Employee employee : collectionEmployee) {
            EmployeeListReportDTO dto = new EmployeeListReportDTO();
            dto.setOfficeLocation(employee.getLocation().getName());
            dto.setFirstName(employee.getPerson().getFirstName());
            dto.setLastName(employee.getPerson().getLastName());
            dto.setCuilCuit(employee.getPerson().getCuilOrCuit());
            dto.setBirthday(DateUtil.getFechaFormateada(employee.getPerson().getDateOfBirth()));
            dto.setFirstWorkingDay(DateUtil.getFechaFormateada(employee.getDateStart()));
            pivotAux = DateUtil.getFechaFormateada(employee.getFirstEmployeeDate());
            dto.setFirstEmployeeDay(pivotAux == null || pivotAux.length() == 0 ? "----" : pivotAux);
            if (employee.getCurrentStatus() != null) {
                dto.setCondition(employee.getCurrentStatus().getStatusType().getDescription());
            }
            pivotAux = employee.getMedicalInsurance();
            dto.setMedicalInsurance(pivotAux == null || pivotAux.length() == 0 ? "----" : pivotAux);
            if (employee.getPerson().getEmergencyContactName() != null
                    && employee.getPerson().getEmergencyContactName().length() != 0
                    && employee.getPerson().getEmergencyContactPhoneNumer() != null
                    && employee.getPerson().getEmergencyContactPhoneNumer().length() != 0) {
                dto.setEmergencyContact(employee.getPerson().getEmergencyContactName() + " / "
                        + employee.getPerson().getEmergencyContactPhoneNumer());
            } else if (employee.getPerson().getEmergencyContactName() != null
                    && employee.getPerson().getEmergencyContactName().length() != 0) {
                dto.setEmergencyContact(employee.getPerson().getEmergencyContactName());
            } else if (employee.getPerson().getEmergencyContactPhoneNumer() != null
                    && employee.getPerson().getEmergencyContactPhoneNumer().length() != 0) {
                dto.setEmergencyContact(employee.getPerson().getEmergencyContactPhoneNumer());
            } else {
                dto.setEmergencyContact("----");
            }

            collection.add(dto);
        }

        if (collection.size() == 0)
            throw new RuntimeException("No data Available for Report");

        Collections.sort((List) collection);
        return collection;
    }

    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    @Override
    protected Map getReportParameters() {
        // TODO Auto-generated method stub
        return null;
    }

}
